package com.log4jviewer.logfile.fields;

/**
 * Class constructs Regex for log's category|class|file|method name field.
 * 
 * @author Apache
 * @author <a href="mailto:rd.ryly@gmail.com">Ruslan Diachenko</a>
 */
public class NamedField extends AbstractField {

    private static final String JAVA_NAMED_CONVENTION = "[A-Za-z_\\$][A-Za-z0-9_\\$]*";

    private int precision;

    private LogFieldName logFieldName;

    public NamedField(final LogFieldName logFieldName, final boolean leftAlign, final boolean rightAlign,
            final int precision) {
        super(logFieldName, leftAlign, rightAlign);
        this.precision = precision;
        this.logFieldName = logFieldName;
    }

    @Override
    public String getRawRegex() {
        String rawRegex = "";

        if (precision == 0) {
            rawRegex = JAVA_NAMED_CONVENTION + "(?:\\." + JAVA_NAMED_CONVENTION + ")*";
        } else if (precision == 1) {
            rawRegex = JAVA_NAMED_CONVENTION;
        } else {
            rawRegex = JAVA_NAMED_CONVENTION + "(?:\\." + JAVA_NAMED_CONVENTION + "){" + String.valueOf(precision - 1)
                    + "}";
        }

        if (LogFieldName.FILE == logFieldName) {
            rawRegex = JAVA_NAMED_CONVENTION + "\\.java";
        }
        return rawRegex;
    }
}